//home模块专享的小仓库
//任何的小仓库:state、mutations、actions、getters
//引入请求函数
import { reqCategory, reqGetBannerList, reqFloorList } from '@/api'
let state = {
  //商品分类的数据,仓库里面数据起始数值不要瞎写【服务器返回的是啥】
  category: [],
  bannerList: [],
  floorList: []
}
let mutations = {
  GETCATEGORY(state, category) {
    state.category = category
  },
  GETBANNERLIST(state, bannerList) {
    state.bannerList = bannerList
  },
  GETFLOORLIST(state, floorList) {
    state.floorList = floorList
  }
}
let actions = {
  //商品分类的actions
  //actions地盘:可不可以书写异步语句
  async getCategory({ commit, state, dispatch }) {
    //获取服务器的数据,存储在vuex仓库中
    //reqCategory函数执行,返回的是Promise对象【pending、成功、失败】
    //await 等待成功的结果
    let result = await reqCategory()
    //判断服务器返回的状态是200->成功
    if (result.code == 200) {
      //提交mutation存储服务器数据
      commit('GETCATEGORY', result.data)
    }
  },
  getBannerList: async ({ commit }) => {
    let result = await reqGetBannerList()
    if (result.code == 200) {
      commit('GETBANNERLIST', result.data)
      // console.log("🚀 ~ file: index.js:36 ~ getBannerList:async ~ result.data:", result.data)
    }
  },
  getFloorList: async ({ commit }) => {
    let result = await reqFloorList()
    if (result.code == 200) {
      //提交mutation存储服务器数据
      commit('GETFLOORLIST', result.data)
    }
  }
}
let getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
